package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.aishua.lib.utils.AsStrUtils;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetPictureBookCommentListBean;
import com.rjwh.dingdong.client.bean.localbean.DrawBookCommentData;
import com.rjwh.dingdong.client.bean.localbean.GetPictureBookCommentListBean;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.DrawBookDetailCommentListModel;
import com.rjwh.dingdong.client.mvp.presenter.IDrawBookDetailCommentListPresenter;
import com.rjwh.dingdong.client.mvp.view.DrawBookDetailCommentListView;
import com.rjwh.dingdong.client.util.ToastUtil;

import java.util.List;

/**
 * @author Kay
 * @ClassName DrawBookDetailCommentListPresenter
 * @Description
 * @date 2016-8-13
 * @history 2016-8-13 author: description:
 */
public class DrawBookDetailCommentListPresenter extends BasePresenter<DrawBookDetailCommentListView> implements IDrawBookDetailCommentListPresenter {
    DrawBookDetailCommentListModel model;
    private SwipyRefreshLayoutDirection mDirection;

    public DrawBookDetailCommentListPresenter(Context context, DrawBookDetailCommentListView mMvpView) {
        super(context, mMvpView);
        model = new DrawBookDetailCommentListModel(this);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (getMvpView() != null) {
            switch (businessType) {
                case NetConstant.GETPICTUREBOOKCOMMENTLIST:
                    getMvpView().setRefreshing(false);
                    ResGetPictureBookCommentListBean getPictureBookCommentListBean = (ResGetPictureBookCommentListBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && getPictureBookCommentListBean != null) {
                        GetPictureBookCommentListBean data = getPictureBookCommentListBean.getData();
                        if (data != null) {
                            List<DrawBookCommentData> drawbookcommentlist = data.getDrawbookcommentlist();
                            if (drawbookcommentlist != null) {
                                if (drawbookcommentlist.isEmpty()) {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                                    if (mDirection == SwipyRefreshLayoutDirection.BOTTOM) {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data_more));
                                    } else {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data));
                                    }
                                } else {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.BOTH);
                                }
                                getMvpView().loadData(isRefresh, drawbookcommentlist);
                            }

                        }
                    } else {
                        getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                        ToastUtil.showToast(getContext(), getContext().getString(R.string.refresh_error));
                    }
                    break;
                default:
                    break;
            }
        }

    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }

    /**
     * 绘本评论列表
     *
     * @param bookid    绘本编码
     * @param pageindex 页面索引
     * @param isRefresh
     * @param direction
     */
    @Override
    public void doGetPictureBookCommentList(int bookid, int pageindex, boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        mDirection = direction;
        model.doGetPictureBookCommentList(bookid, pageindex, isRefresh);
    }
}
